package com.wanjian.sak.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanjian.sak.R;
import com.wanjian.sak.config.Config;
import com.wanjian.sak.config.Item;
import com.wanjian.sak.layer.Layer;
import com.wanjian.sak.layer.LayerRoot;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class OptPanelView extends LinearLayout {
    private final GridView function;
    private Config config;
    private OnClickListener confirmListener;

    public OptPanelView(@NonNull Context context) {
        this(context, null);
    }

    public OptPanelView(@NonNull final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.sak_opt_panel_view, this);

        function = findViewById(R.id.function);

        findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmListener != null) {
                    confirmListener.onClick(v);
                }
            }
        });
    }


    public void setConfirmListener(OnClickListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void attachConfig(Config config) {
        this.config = config;
        setFunctions();
        //setSizeConverter();
        //changeSizeConvert(ISizeConverter.CONVERTER );
    }




    private void setFunctions() {
        setAdapter(config.getLayerList());
        function.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Item item = config.getLayerList().get(position);
                item.setEnable(!item.isEnable());
                Holder holder = (Holder) view.getTag(R.layout.sak_function_item);
                if (item.isEnable()) {
                    holder.check.setVisibility(VISIBLE);
                } else {
                    holder.check.setVisibility(GONE);
                }
                update();
            }
        });
    }

    private void update() {
        for (WeakReference<LayerRoot> reference : weakReferences) {
            LayerRoot root = reference.get();
            if (root == null) {
                continue;
            }
            List<Layer> layers = root.getLayers();
            for (Item item : config.getLayerList()) {
                Layer layer = getLayerByType(layers, item.layerType);
                if (layer == null) {
                    continue;
                }
                if (layer.isEnable() != item.isEnable()) {
                    layer.enable(item.isEnable());
                    if (layer.isEnable()) {
                        layers.remove(layer);
                        layers.add(layer);
                    } else {
                        layers.remove(layer);
                        layers.add(0, layer);
                    }
                    break;
                }
            }
        }
    }

    private Layer getLayerByType(List<Layer> layers, Class<? extends Layer> layerType) {
        for (Layer layer : layers) {
            if (layer.getClass() == layerType) {
                return layer;
            }
        }
        return null;
    }

    private void setAdapter(final List<Item> iLayers) {
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        function.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return iLayers.size();
            }

            @Override
            public Item getItem(int position) {
                return iLayers.get(position);
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.sak_function_item, function, false);
                    Holder holder = new Holder();
                    holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                    holder.title = (TextView) convertView.findViewById(R.id.title);
                    holder.check = convertView.findViewById(R.id.check);
                    convertView.setTag(R.layout.sak_function_item, holder);
                }
                Holder holder = (Holder) convertView.getTag(R.layout.sak_function_item);
                Item layer = getItem(position);
                holder.title.setText(layer.name);
                holder.icon.setImageDrawable(layer.icon);
                if (layer.isEnable()) {
                    holder.check.setVisibility(VISIBLE);
                } else {
                    holder.check.setVisibility(GONE);
                }
                return convertView;
            }
        });

    }


    private List<WeakReference<LayerRoot>> weakReferences = new ArrayList<>();

    public void add(LayerRoot layerRoot) {
        weakReferences.add(new WeakReference<>(layerRoot));
    }

    public void enableIfNeeded(LayerRoot layerRoot) {
        List<Layer> layers = layerRoot.getLayers();
        for (Item item : config.getLayerList()) {
            Layer layer = getLayerByType(layers, item.layerType);
            if (layer == null) {
                continue;
            }
            if (layer.isEnable() != item.isEnable()) {
                layer.enable(item.isEnable());
            }
        }
    }

    class Holder {
        ImageView icon;
        TextView title;
        View check;

    }
}
